package com.diyunapp.xilinapp.me;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeNameActivity extends StatusBarFragmentActivity {

    @Bind(R.id.user_name_et)
    EditText userNameEt;
    @Bind(R.id.update_name)
    TextView updateName;
    @Bind(R.id.go_back)
    ImageView goBack;
    private View headView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_name);
        ButterKnife.bind(this);
        headView = findViewById(R.id.chaname_menubar_inc);
        super.MenuWidget(headView);
    }

    @OnClick({R.id.update_name,R.id.go_back})
    public void onClick(View view) {
            switch (view.getId()){
                case R.id.update_name:
                    update();
                    break;
                case R.id.go_back:
                    finish();
                    break;
            }

    }

    public void update() {
        if (TextUtils.isEmpty(userNameEt.getText().toString())) {
            Toast.makeText(this, "未输入姓名！", Toast.LENGTH_SHORT).show();
            return;
        }
        else{
            initUpdate();
        }
    }

    public void initUpdate() {
        String url = ""+MainActivity.url+"act=goods&op=real_name" +
                "&username="+ MainActivity.myLogin.getUsername()+"&real_name="+userNameEt.getText().toString().trim()+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
                            String mes=resultObject.optString("date");
//                            toast(String.valueOf(result));
                            if (result == 200) {
                                    toast(mes);
                                Log.i("xiaogalade","123456789");
                                String realname=userNameEt.getText().toString().trim();
                                SharedPreferences user_info=getSharedPreferences("xilinshangchengSP",MODE_PRIVATE);
                                user_info.edit().putString("realname",realname).commit();
                                setResult(3);
                                finish();

                            } else {
                                toast(mes);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");

                    }
                });
    }
}
